<?php

namespace App\Models\Purchase;

use App\Models\Custom\Custom;
use App\Models\Manger\Manger;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class PurchasePayOrderApprove extends Model
{
    protected $table = 'purchase_pay_order_approve';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];


    // 自定义关系方法：根据 approve_id1 关联到 Manger 表
    public function approver1()
    {
        return $this->belongsTo(Manger::class, 'approve_id1', 'id')->select('id', 'realname');
    }

    // 自定义关系方法：根据 approve_id2 关联到 Manger 表
    public function approver2()
    {
        return $this->belongsTo(Manger::class, 'approve_id2', 'id')->select('id', 'realname');
    }


    public function mangerinfo()
    {
        return $this->belongsTo(Manger::class, 'manger_id', 'id')->select('id', 'realname');
    }
    public function custom(){
        return $this->hasOne(Custom::class,'id','custom_id');
    }


    /**
     *获取列表
     */
    public static function getList(Request $request, $cpid, $mangerinfo)
    {
        $query = self::with(['mangerinfo','custom'])->where(['cpid' => $cpid, 'is_del' => 1])->orderBy('create_at', 'desc');

        $manger_id = $mangerinfo['id'];

        if($mangerinfo['utype']==6){
            $query->where('manger_id', $manger_id);
        }elseif ($mangerinfo['utype'] == 8){
            //2025-08-13  bugid是19643
            //出纳不查询被拒绝或者撤审的数据

            //2025-11-4  出纳只查询审核通过的数据
            $query->where('audit_status', '=',3);
        }else{
            if($mangerinfo['utype']==4){
                $query->where('approve_id1', $manger_id);
            }

            if($mangerinfo['utype']==2){
                $query->where('approve_id2', $manger_id);
            }
        }
        $is_tplus_sync = $request->is_tplus_sync;
        if ($is_tplus_sync) {
            if(is_array($is_tplus_sync)){
                $query->whereIn('is_tplus_sync', $is_tplus_sync);
            }else{
                $query->where('is_tplus_sync', $is_tplus_sync);
            }

        }


        $xsmname = $request->xsmname;
        if ($xsmname) {
            $query->whereHas('mangerinfo', function ($query) use ($xsmname) {
                //$query->where('realname', $xsmname);
                $query->where('realname','like' , "%".$xsmname."%");
            });
        }


        $pa_type = $request->pa_type;
        if (in_array($pa_type, [1, 2, 3, 4])) {
            $query->where('pa_type', $pa_type);
        }
        $urgent_remark = $request->urgent_remark;
        if ($urgent_remark>0) {
            $query->where('urgent_remark', $urgent_remark);
        }


        $custom_id = $request->custom_id;
        if ($custom_id>0) {
            $query->where('custom_id', $custom_id);
        }




        $pa_settle_company_name = $request->pa_settle_company_name; //供应商名称
        if ($pa_settle_company_name != '') {
            $query->where('pa_settle_company_name', 'like', '%' . $pa_settle_company_name . '%');
        }

        $company_name = $request->company_name; //供应商名称
        if ($company_name != '') {
            $query->where('company_name', 'like', '%' . $company_name . '%');
        }

        $pay_sn_code = $request->pay_sn_code; //供应商名称
        if ($pay_sn_code != '') {
            $query->where('pay_sn_code', 'like', '%' . $pay_sn_code . '%');
        }

        $settle_model=intval($request->settle_model);

        if($settle_model>0){//結算模式
            $query->where('settle_model', $settle_model);
        }

        $audit_status = $request->audit_status;
        if (in_array($audit_status, [1, 2, 3, 4,5])) {
            $query->where('audit_status', $audit_status);
        }

        $start_time = strtotime($request->start_time)>0?strtotime($request->start_time):intval($request->start_time);
        $end_time = strtotime($request->end_time)>0?strtotime($request->end_time):intval($request->end_time);
        if ($start_time > 0 && $end_time > 0) {
            //$query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
            $query->where('pay_order_date', '>=', $start_time)->where('pay_order_date', '<=', $end_time);
        }


        $fee_min=$request->fee_min;
        $fee_max=$request->fee_max;

        if($fee_min>0 && $fee_max>0){
            $fee_min = $fee_min * 100;
            $fee_max = $fee_max * 100;
            $query->whereBetween('pay_money',[$fee_min,$fee_max]);
        }


        $limit = $request->limit ?? 10;
        $list = $query->paginate($limit)->toArray();
        return paginateDataToArray($list);
    }


}
